<?php

class saveDeleteResearchPublicationAction extends basePimAction
{

    /**
     * @param sfForm $form
     * @return
     */
    public function setResearchPublicationForm(sfForm $form)
    {
        if (is_null($this->researchPublicationForm)) {
            $this->researchPublicationForm = $form;
        }
    }

    public function execute($request)
    {

        $employeeResearchPublication = $request->getParameter('employeeResearchPublication');
        $empNumber = (isset($employeeResearchPublication['emp_number'])) ? $employeeResearchPublication['emp_number'] : $request->getParameter('empNumber');

        if (!$this->IsActionAccessible($empNumber)) {
            $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
        }

        $this->employeeResearchPublicationPermissions = $this->getDataGroupPermissions('qualification_research_publication', $empNumber);

        $this->setResearchPublicationForm(new EmployeeResearchPublicationForm(array(), array('empNumber' => $empNumber, 'employeeResearchPublicationPermissions' => $this->employeeResearchPublicationPermissions), true));

        //this is to save research publication
        if ($request->isMethod('post')) {
            if ($request->getParameter('option') == "save") {
                if ($this->employeeResearchPublicationPermissions->canCreate() || $this->employeeResearchPublicationPermissions->canUpdate()) {

                    $this->researchPublicationForm->bind($request->getParameter($this->researchPublicationForm->getName()));

                    if ($this->researchPublicationForm->isValid()) {
                        $researchPublication = $this->getResearchPublication($this->researchPublicationForm);

                        $this->getEmployeeService()->saveEmployeeResearchPublication($researchPublication);
                        $this->getUser()->setFlash('researchPublication.success', __(TopLevelMessages::SAVE_SUCCESS));
                    } else {
                        $this->getUser()->setFlash('researchPublication.warning', __('Form Validation Failed.'));
                    }
                }
            }

            //this is to delete research publication
            if ($request->getParameter('option') == "delete") {
                if ($this->employeeResearchPublicationPermissions->canDelete()) {
                    $deleteIds = $request->getParameter('delResearchPublication');

                    if (count($deleteIds) > 0) {
                        $this->getEmployeeService()->deleteEmployeeResearchPublicationRecords($empNumber, $request->getParameter('delResearchPublication'));
                        $this->getUser()->setFlash('researchPublication.success', __(TopLevelMessages::DELETE_SUCCESS));
                    }
                }
            }
        }
        $this->getUser()->setFlash('qualificationSection', 'researchPublication');
        $this->redirect('pim/viewQualifications?empNumber=' . $empNumber . '#researchPublication');
    }

    private function getResearchPublication(sfForm $form)
    {

        $post = $form->getValues();

        $researchPublication = $this->getEmployeeService()->getEmployeeResearchPublicationRecords($post['emp_number'], $post['seqno']);

        if (!$researchPublication instanceof EmployeeResearchPublication) {
            $researchPublication = new EmployeeResearchPublication();
        }

        $researchPublication->emp_number = $post['emp_number'];
        $researchPublication->seqno = $post['seqno'];
        $researchPublication->title = $post['title'];
        $researchPublication->authors = $post['authors'];
        $researchPublication->publisher = $post['publisher'];
        $researchPublication->publishing_year = $post['publishing_year'];

        return $researchPublication;
    }

}

?>